package com.gitee.wsl.func.procedure.sample

import com.gitee.wsl.func.predicate.Predicate
import com.gitee.wsl.func.predicate.alwaysTrue
import com.gitee.wsl.func.procedure.Procedure
import kotlin.jvm.JvmOverloads

/**
 * Applies a predicate to an object and increments a count if it returns true.
 */
class CountProcedure<T> @JvmOverloads constructor(
    private val predicate: Predicate<in T> = Predicate.alwaysTrue()
) : Procedure<T> {

    var count: Int = 0
        private set

    override fun value(each: T) {
        if (this.predicate.accept(each)) {
            this.count++
        }
    }

}
